﻿using UnityEngine;
using System.Collections;

public class UIDepth : MonoBehaviour
{
    public int order;
    public string layerName = "Default";
    public bool isUI = true;

    Canvas canvas;
    Renderer[] renders;

    void Awake()
    {
        if (isUI)
        {
            canvas = GetComponent<Canvas>();
            if (canvas == null)
            {
                canvas = gameObject.AddComponent<Canvas>();
            }
        }
        else
        {
            renders = GetComponentsInChildren<Renderer>();
        }
    }

    void Start()
    {
        if (isUI)
        {
            canvas.overrideSorting = true;
            canvas.sortingOrder = order;
        }
        else
        {
            for (int i = 0; i < renders.Length; ++i)
            {
                renders[i].sortingLayerName = layerName;
                renders[i].sortingOrder = order;
            }
        }
    }
}
